﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using XeBus.BusinessObject;

namespace XeBus.DataAccess
{
    public class TramDungDAO
    {
        private ConnectData _conn;

        public TramDungDAO()
        {
            _conn = new ConnectData();
        }

        // Hàm thêm mới một trạm dừng
        public void THEM_TRAM_DUNG(TramDung td)
        {

            SqlParameter[] param = new SqlParameter[5];

            param[0] = new SqlParameter("P_SO_HIEU_TRAM", SqlDbType.NVarChar);
            param[0].Value = td.So_Hieu_Tram;

            param[1] = new SqlParameter("P_TEN_TRAM", SqlDbType.NVarChar);
            param[1].Value = td.Ten_Tram;

            param[2] = new SqlParameter("P_DIA_CHI_TRAM", SqlDbType.NVarChar);
            param[2].Value = td.Dia_Chi_Tram;
            

            param[3] = new SqlParameter("P_VI_TRI_DIA_LY", SqlDbType.NVarChar);
            param[3].Value = td.Vi_Tri_Dia_Ly;
            

            param[4] = new SqlParameter("P_MA_CON_DUONG", SqlDbType.Int);
            if (td.Ma_Con_Duong.Length > 0)
                param[4].Value = td.Ma_Con_Duong;
            else
                param[4].Value = DBNull.Value;

            _conn.ExecuteNonQuery("THEM_TRAMDUNG", param);
        }

        public DataTable TramDung()
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM TramDung", null);
        }

        public DataTable TramDung(string ma_con_duong)
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM TramDung WHERE Ma_Con_Duong=" + ma_con_duong + "", null);
        }

        public DataTable TramDungTheoID(string id)
        {
            return _conn.ExecuteSelectQuery("SELECT * FROM TramDung WHERE Ma_Tram_Dung=" + id + "", null);
        }

        public void SUA_TRAM_DUNG(TramDung td)
        {
            SqlParameter[] param = new SqlParameter[5];

            param[0] = new SqlParameter("P_SO_HIEU_TRAM", SqlDbType.NVarChar);
            param[0].Value = td.So_Hieu_Tram;

            param[1] = new SqlParameter("P_TEN_TRAM", SqlDbType.NVarChar);
            param[1].Value = td.Ten_Tram;

            param[2] = new SqlParameter("P_DIA_CHI_TRAM", SqlDbType.NVarChar);
            param[2].Value = td.Dia_Chi_Tram;


            param[3] = new SqlParameter("P_VI_TRI_DIA_LY", SqlDbType.NVarChar);
            param[3].Value = td.Vi_Tri_Dia_Ly;


            param[4] = new SqlParameter("P_MA_TRAM_DUNG", SqlDbType.Int);
            if (td.Ma_Tram_Dung.Length > 0)
                param[4].Value = td.Ma_Tram_Dung;
            else
                param[4].Value = DBNull.Value;

            _conn.ExecuteNonQuery("SUA_TRAMDUNG", param);
        }

        public void XOA(string id)
        {
            _conn.ExecuteNonQueryString("DELETE FROM TramDung WHERE Ma_Tram_Dung=" + id + ";");
        }

        public DataTable Ma_Con_Duong()
        {
            return _conn.ExecuteSelectQuery("SELECT Ma_Con_Duong FROM ConDuong", null);
        }
    }
}
